package com.icinfo.cloud.provider.punish.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.icinfo.cloud.common.security.util.UserUtil;
import com.icinfo.cloud.provider.common.constant.Constant;
import com.icinfo.cloud.provider.common.enums.YesOrNoEnum;
import com.icinfo.cloud.provider.exception.BusinessException;
import com.icinfo.cloud.provider.punish.approval.vo.LineTerritoryInfoVo;
import com.icinfo.cloud.provider.punish.config.model.SysDepartParam;
import com.icinfo.cloud.provider.punish.config.vo.LineCaseHandleInfoVo;
import com.icinfo.cloud.provider.punish.mapper.SysDepartParamMapper;
import com.icinfo.cloud.provider.punish.service.ISysDepartParamService;
import com.icinfo.cloud.provider.punish.service.IXzzfInterfaceService;
import com.icinfo.cloud.xzcf.provider.member.line.dto.LineDTO;
import com.icinfo.cloud.xzcf.provider.member.userinfo.dto.UserDTO;
import com.icinfo.framework.core.service.support.MyBatisServiceSupport;
import com.icinfo.framework.mybatis.mapper.entity.Example;
import com.icinfo.framework.tools.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 描述: 部门参数 sys_depart_param 对应的Service接口实现类.<br>
 *
 * @author framework generator
 * @date 2022年03月24日
 */
@Service
@Slf4j
public class SysDepartParamServiceImpl extends MyBatisServiceSupport implements ISysDepartParamService {
    @Resource
    private SysDepartParamMapper sysDepartParamMapper;
    @Resource
    private IXzzfInterfaceService xzzfInterfaceService;
    /**
     * 根据机构编码获取部门配置信息
     *
     * @param orgCode
     * @return {@link SysDepartParam}
     * @author liyafeng
     * @date 2022/3/31
     */
    @Override
    public SysDepartParam getSysDepartParamByOrgCode(String orgCode,String deptCode) {
        /*if (StringUtils.isNotEmpty(orgCode)) {
            *//*Example queryExample = new Example(SysDepartParam.class);
            queryExample.createCriteria().andEqualTo("orgCode", handleDeptCode);
            List<SysDepartParam> sysDepartParamList = sysDepartParamMapper.selectByExample(queryExample);
            if (sysDepartParamList.size() > 0) {
                return sysDepartParamList.get(0);
            } else {
                throw new BusinessException("部门参数配置信息为空！");
            }*//*

        } else {
            throw new BusinessException("非法传值，部门编码deptCode为空！");
        }*/
        return getSysDepartParamByOrgCodeAndDeptCode(orgCode,deptCode);
    }
    /**
     * 描述：将条线配置组装成集合对象
     *
     * @param lineConfig 条线配置（json字符串）
     * @return {@link List< LineCaseHandleInfoVo >} 组装好的条线配置集合
     * @author xiangyuyu
     * @date 2022-05-20 14:21
     */
    @Override
    public List<LineCaseHandleInfoVo> assembleLineConfigToList(String lineConfig) {
        List<LineCaseHandleInfoVo> lineCaseHandleInfoNew = new ArrayList<>();
        if (!ObjectUtils.isEmpty(lineConfig) && lineConfig.startsWith(Constant.STRING_LEFT_PARENTHESE)
                && lineConfig.endsWith(Constant.STRING_RIGHT_PARENTHESE)) {
            JSONArray jsonArray = JSONArray.parseArray(lineConfig);
            if (!ObjectUtils.isEmpty(jsonArray) && jsonArray.size() > 0) {
                //解析
                jsonArray.forEach(e -> {
                    JSONObject jsonoBject = (JSONObject) e;
                    LineCaseHandleInfoVo lineConfigInfo = jsonoBject.toJavaObject(LineCaseHandleInfoVo.class);
                    lineConfigInfo.setLineCaseHandleStatusName(YesOrNoEnum.getValueByCode(lineConfigInfo.getLineCaseHandleStatusCode()));
                    lineCaseHandleInfoNew.add(lineConfigInfo);
                });
            }
        }
        return lineCaseHandleInfoNew;
    }

    @Override
    public List<LineTerritoryInfoVo> getLineInfoListByOrgCode(String orgCode) {
        UserDTO userDTO= UserUtil.getUserInfo();
        String deptCode=userDTO.getDeptCode();
        SysDepartParam sysDepartParam = getSysDepartParamByOrgCodeAndDeptCode(orgCode,deptCode);
        String lineConfig = sysDepartParam.getLineConfig();
        List<LineTerritoryInfoVo> lineTerritoryInfoVoList =new ArrayList<>();
        List<LineCaseHandleInfoVo> lineCaseHandleInfoVos = assembleLineConfigToList(lineConfig);
        if (ObjectUtil.isEmpty(lineConfig)){
            throw new BusinessException("暂未配置条线信息，请联系管理员");
        }
        if (ObjectUtil.isNotEmpty(lineCaseHandleInfoVos) && lineCaseHandleInfoVos.size()>0){
            for (LineCaseHandleInfoVo lineCaseHandleInfoVo:lineCaseHandleInfoVos){
                if (Constant.STRING_1.equals(lineCaseHandleInfoVo.getLineCaseHandleStatusCode())){
                    LineTerritoryInfoVo lineTerritoryInfoVo =new LineTerritoryInfoVo();
                    lineTerritoryInfoVo.setLineCaseHandleStatusCode(lineCaseHandleInfoVo.getLineCaseHandleStatusCode());
                    lineTerritoryInfoVo.setLineCaseHandleStatusName(lineCaseHandleInfoVo.getLineCaseHandleStatusName());
                    lineTerritoryInfoVo.setLineCode(lineCaseHandleInfoVo.getLineCode());
                    lineTerritoryInfoVo.setLineName(lineCaseHandleInfoVo.getLineName());
                    lineTerritoryInfoVo.setLineDocNumber(lineCaseHandleInfoVo.getLineDocNumber());
                    lineTerritoryInfoVoList.add(lineTerritoryInfoVo);
                }
            }
        }
        if (ObjectUtil.isEmpty(lineTerritoryInfoVoList) && lineTerritoryInfoVoList.size()==0){
            throw new com.icinfo.cloud.provider.exception.BusinessException("暂未配置有效的条线信息，请联系管理员");
        }
        return lineTerritoryInfoVoList;
    }

    @Override
    public LineTerritoryInfoVo getLineInfoByOrgCodeAndLineCode(String orgCode, String lineCode) {
        UserDTO userDTO=UserUtil.getUserInfo();
        SysDepartParam sysDepartParam = getSysDepartParamByOrgCode(orgCode,userDTO.getDeptCode());
        String lineConfig = sysDepartParam.getLineConfig();
        List<LineCaseHandleInfoVo> lineCaseHandleInfoVos = assembleLineConfigToList(lineConfig);
        if (ObjectUtil.isEmpty(lineCaseHandleInfoVos)){
            throw new BusinessException("暂未配置条线信息，请联系管理员");
        }
        lineCaseHandleInfoVos=  lineCaseHandleInfoVos.stream().filter(e->e.getLineCode().equals(lineCode)).collect(Collectors.toList());
        if (lineCaseHandleInfoVos.size()>0){
            LineCaseHandleInfoVo lineCaseHandleInfoVo=lineCaseHandleInfoVos.get(0);

            LineTerritoryInfoVo lineTerritoryInfoVo = new  LineTerritoryInfoVo();
            LineDTO lineDto = xzzfInterfaceService.getLineInfoByLineCode(lineCode);
            lineTerritoryInfoVo.setTerritoryCode(lineDto.getTerritoryCode());
            lineTerritoryInfoVo.setTerritoryName(lineDto.getTerritoryName());
            lineTerritoryInfoVo.setLineCaseHandleStatusCode(lineCaseHandleInfoVo.getLineCaseHandleStatusCode());
            lineTerritoryInfoVo.setLineCaseHandleStatusName(lineCaseHandleInfoVo.getLineCaseHandleStatusName());
            lineTerritoryInfoVo.setLineCode(lineCaseHandleInfoVo.getLineCode());
            lineTerritoryInfoVo.setLineName(lineCaseHandleInfoVo.getLineName());
            lineTerritoryInfoVo.setLineDocNumber(lineCaseHandleInfoVo.getLineDocNumber());
            return lineTerritoryInfoVo;
        }
        return null;
    }
    /**
     * 根据部门编码与机构编码获取对应的参数配置
     * @author liyafeng
     * @date 2022/11/23
     * @param orgCode
     * @param deptCode
     * @return {@link SysDepartParam}
     */
    @Override
    public SysDepartParam getSysDepartParamByOrgCodeAndDeptCode(String orgCode,String deptCode) {
        List<SysDepartParam> sysDepartParams=new ArrayList<>();
        SysDepartParam sysDepartParam=new SysDepartParam();
        String isConfigDept=Constant.STRING_0;
        Example example = new Example(SysDepartParam.class);
        example.createCriteria().andEqualTo("orgCode",orgCode).andCondition("(dept_code is null or dept_code='') ").andEqualTo("isDeleted",Constant.STRING_0);
        sysDepartParams = sysDepartParamMapper.selectByExample(example);
        if (!ObjectUtils.isEmpty(sysDepartParams)){
            sysDepartParam = sysDepartParams.get(Constant.INTEGER_0);
            isConfigDept=sysDepartParams.get(Constant.INTEGER_0).getIsConfigDept();
        }else{
            throw new com.icinfo.cloud.provider.common.exception.BusinessException("单位参数配置信息为空！");
        }
        if(Constant.STRING_1.equals(isConfigDept)&&StringUtils.isNotBlank(deptCode)){
            Example deptExample = new Example(SysDepartParam.class);
            deptExample.createCriteria().andEqualTo("orgCode",orgCode).andEqualTo("deptCode",deptCode).andEqualTo("isDeleted",Constant.STRING_0);
            sysDepartParams = sysDepartParamMapper.selectByExample(deptExample);
            if(!ObjectUtils.isEmpty(sysDepartParams)){
                sysDepartParam=sysDepartParams.get(0);
            }
        }
        return sysDepartParam;
    }
    @Override
    public LineTerritoryInfoVo getLineInfoByOrgCodeAndLineCodeNumber(String orgCode, String deptCode, String lineCode) {
        SysDepartParam sysDepartParam = getSysDepartParamByOrgCode(orgCode,deptCode);
        String lineConfig = sysDepartParam.getLineConfig();
        List<LineCaseHandleInfoVo> lineCaseHandleInfoVos = assembleLineConfigToList(lineConfig);
        if (ObjectUtil.isEmpty(lineCaseHandleInfoVos)){
            throw new com.icinfo.cloud.provider.common.exception.BusinessException("暂未配置条线信息，请联系管理员");
        }
        lineCaseHandleInfoVos=  lineCaseHandleInfoVos.stream().filter(e->e.getLineCode().equals(lineCode)).collect(Collectors.toList());
        if (ObjectUtil.isEmpty(lineCaseHandleInfoVos)){
            throw new com.icinfo.cloud.provider.common.exception.BusinessException("办案机构暂未配置条线信息，请联系管理员");
        }
        lineCaseHandleInfoVos=  lineCaseHandleInfoVos.stream().filter(e->e.getLineCode().equals(lineCode)).collect(Collectors.toList());
        if (lineCaseHandleInfoVos.size()>0){
            LineCaseHandleInfoVo lineCaseHandleInfoVo=lineCaseHandleInfoVos.get(0);
            LineTerritoryInfoVo lineTerritoryInfoVo = new  LineTerritoryInfoVo();
            //LineDTO lineDto = xzzfInterfaceService.getLineInfoByLineCode(lineCode);
            //lineTerritoryInfoVo.setTerritoryCode(lineDto.getTerritoryCode());
            //lineTerritoryInfoVo.setTerritoryName(lineDto.getTerritoryName());
            lineTerritoryInfoVo.setLineCaseHandleStatusCode(lineCaseHandleInfoVo.getLineCaseHandleStatusCode());
            lineTerritoryInfoVo.setLineCaseHandleStatusName(lineCaseHandleInfoVo.getLineCaseHandleStatusName());
            lineTerritoryInfoVo.setLineCode(lineCaseHandleInfoVo.getLineCode());
            lineTerritoryInfoVo.setLineName(lineCaseHandleInfoVo.getLineName());
            lineTerritoryInfoVo.setLineDocNumber(lineCaseHandleInfoVo.getLineDocNumber());
            return lineTerritoryInfoVo;
        }
        return null;
    }

    @Override
    public SysDepartParam getPublicSecurityEntrustSysDepartParamByOrgCodeAndDeptCode(String orgCode, String deptCode) {
        if (StrUtil.isBlank(orgCode)) {
            throw new BusinessException("委托执法机构代码为空！");
        }
        SysDepartParam param = new SysDepartParam();
        param.setOrgCode(orgCode);
        if (StrUtil.isNotBlank(deptCode)) {
            param.setDeptCode(deptCode);
        }
        SysDepartParam sysDepartParam = sysDepartParamMapper.selectOne(param);
        if (Objects.isNull(sysDepartParam)) {
            throw new BusinessException("无委托执法相关配置信息！");
        }
        return sysDepartParam;
    }
}
